package models;

import com.avaje.ebean.Expr;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.h2.engine.User;
import play.Logger;
import play.data.format.Formats;
import play.db.ebean.Model;
import play.libs.Json;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by richard on 03/04/14.
 */
@Entity

public class EvenementAgenda extends Model {
    public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    public ObjectNode toJson(){
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);

        ObjectNode result = Json.newObject();
        result.put("id",id);
        result.put("dateDebut",sdf.format(this.dateDebut));
        result.put("dateFin",sdf.format(this.dateFin));
        result.put("employe_id",this.employe.getId());
        result.put("service_id",this.service.getId());
        return result;
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDateDebut() {
        return dateDebut;
    }

    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

    public Date getDateFin() {
        return dateFin;
    }

    public void setDateFin(Date dateFin) {
        this.dateFin = dateFin;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    int id;

    @Formats.DateTime(pattern="dd/MM/yyyy hh:mm:ss")
    public Date dateDebut = new Date();
    @Formats.DateTime(pattern="dd/MM/yyyy hh:mm:ss")
    public Date dateFin = new Date();

    @Column(name="employe_id")
    @ManyToOne
    public Employe employe;

    @Column(name="service_id")
    @ManyToOne
    public Service service;

    //ORM
    public static Finder<Long,EvenementAgenda> find = new Finder<Long,EvenementAgenda>(
            Long.class,EvenementAgenda.class
    );

    public static List<EvenementAgenda> findForIdInIntervalle(Date datedebut,Date dateFin,int service_id) {
        List<EvenementAgenda> result = find.where().and(
                //com.avaje.ebean.Expr.eq("service_id", service_id),
                Expr.ge("date_debut", datedebut),

                Expr.le("date_fin", dateFin)
        ).eq("service_id", service_id).orderBy("date_debut").findList();
        Logger.debug("Find intervalle INFO " + result);
        return result;
    }


}
